var formType;
mini.parse();

var form = new mini.Form("teamForm");

var captainList;
var divisionList;


function beforenodeselect(e) {
    //禁止选中父节点
    if (e.isLeaf == false) e.cancel = true;
}

function onCloseClick(e) {
    var obj = e.sender;
    obj.setText("");
    obj.setValue("");
}

$(function () {
    divisionList = JSON.parse(localStorage.getItem("divisionList"));
    initCaptain();
    initCaptainData(captainList,'');
  
    if(divisionList == null){
        initDivision();
        initDivisionData(divisionList,'');
    }else{
        initDivisionData(divisionList,'');
    }
})


function initCaptain(){
    $.ajax({
        url: GatewayUrl() + '/team/captainList',
        type: 'get',
        async: false,
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                captainList = dt.data;
            }
        }
    })
}

function initCaptainData(data,captainId) {
    var obj = mini.get("Captain");
    obj.setData(data);
    if(captainId !== ''){
        obj.setValue(captainId)
    }
}


function initDivision(){
    $.ajax({
        url: GatewayUrl() + '/team/divisionList',
        type: 'get',
        async: false,
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function(dt){
            if(dt.code === 200){
                localStorage.setItem("divisionList",JSON.stringify(dt.data));
                divisionList = dt.data;
            }
        }
    })
}


function initDivisionData(data,did){
    var obj = mini.get("Division");
    obj.loadList(data,"did","pid");
    if(did !== ''){
        obj.setValue(did)
    }
}




function SaveData() {
    var o = form.getData();
    form.validate();
    if (form.isValid() == false) return;
    if(o.captainId !== ''){
        $.ajax({
            url: GatewayUrl() + "/team/add",
            type: 'post',
            contentType: "application/json",
            data: JSON.stringify(o),
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    CloseWindow("save");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
    }
}


function UpdateData() {
    var o = form.getData();

    form.validate();
    if (form.isValid() == false) return;
        $.ajax({
            url: GatewayUrl() + "/team/update",
            type: 'put',
            contentType: "application/json",
            data: JSON.stringify(o),
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    CloseWindow("save");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
}

////////////////////
//标准方法接口定义
function SetData(data) {
    if (data.action == "edit") {
        formType = 'edit';
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        $.ajax({
            url: GatewayUrl() + "/team/getTeam/" + data.tid,
            type: 'get',
            async: false,
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    var o = mini.decode(dt.data);
                    form.setData(o);
                    initCaptainData(captainList,dt.data.captainId);
                    initDivisionData(divisionList,dt.data.did);
                }
            }
        })

    } else {
        formType = 'add';
    }
}




function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    if (formType == 'add') {
        mini.confirm("确定添加？", "确定？",
            function (action) {
                if (action == "ok") {
                    SaveData();
                }
            }
        );
    } else {
        mini.confirm("确定修改？", "确定？",
            function (action) {
                if (action == "ok") {
                    UpdateData();
                }
            }
        );
    }

}
function onCancel(e) {
    CloseWindow("cancel");
}